package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * RESTful风格的一些注解的使用
 * @author stone
 * @date 2022/08/08 16:59
 */
@RestController
public class RestfulAnnotationController {

    // localhost:8080/questions/1024
    //大括号里的值就是占位符的名称，你也可以写多个占位符
    // 占位符的名称和@PathVariable的value属性值相同
    @RequestMapping("questions/{id}")
    public BaseRespVo questions(@PathVariable("id")Integer id) {
        return BaseRespVo.ok(null,id);
    }

    // 可以使用但是没有必要，因为我们之前写形参名就是来接收特定名称的请求参数 → 要求你一定要携带对应的请求参数
    // localhost:8080/questions2?text=hello&answer=world
    @RequestMapping("questions2")
    public BaseRespVo questions2(@RequestParam("text") String param1,
                                 @RequestParam("answer") String param2) {
        return BaseRespVo.ok(null);
    }

    //localhost:8080/header
    @RequestMapping("header")
    public BaseRespVo header(@RequestHeader("Host") String host,
                             @RequestHeader("Accept") String[] accept) {
                             //@RequestHeader("Accept") String accept) {
        //String[] array = accept.split(",");
        return BaseRespVo.ok(null, accept);
    }

    @RequestMapping("cookie/value")
    public BaseRespVo cookieValue(@CookieValue("ligenli") String value) {

        return BaseRespVo.ok(null, value);
    }

    //localhost:8080/session/put/ligenli
    @RequestMapping("session/put/{name}")
    public BaseRespVo sessionPut(@PathVariable("name")String name, HttpSession session) {
        session.setAttribute("name", name); // "name" → “ligenli”
        return BaseRespVo.ok(null, name);
    }

    //localhost:8080/session/get
    @RequestMapping("session/get")
    public BaseRespVo sessionGet(@SessionAttribute("name")String name) {
        return BaseRespVo.ok(null, name);
    }
}
